import { t } from 'i18next'
import { Alert } from '@/components/Alert'
import { SendSms, SmsEntity } from '@/api/manager/sms/interface'
import { Mt, MtEntity } from '@/api/manager/mt'
//短消息终端
export interface SmsTerModel {
	mcc: Mt
	status: {
		checked: boolean
	}
	mtKey: Mt['mtKey']
	number: string
}
//短消息
export interface SmsEntityEdit {
	id: string
	msg: string
	isShowEdit: boolean
}
export type TabItem = {
	value: number
	label: string
	width?: number
}
export type ListModel = {
	value: number
	label: string
	id: number
}
export const TAB: TabItem[] = [
	{ value: 1, label: t('全部与会方'), width: 100 },
	{ value: 2, label: t('自定义'), width: 100 },
]
export const PositionOption: ListModel[] = [
	{
		value: 0,
		label: t('自右至左滚动'),
		id: 0,
	},
	{
		value: 1,
		label: t('翻页滚动'),
		id: 1,
	},
	{
		value: 2,
		label: t('全页滚动'),
		id: 2,
	},
	{
		value: 3,
		label: t('静止'),
		id: 3,
	},
]
export const SpeedOption: ListModel[] = [
	{
		value: 1,
		label: t('慢速'),
		id: 1,
	},
	{
		value: 2,
		label: t('中速'),
		id: 2,
	},
	{
		value: 3,
		label: t('快速'),
		id: 3,
	},
]
export type SmsListModel = SmsTerModel[]
export type SmsEntityList = (SmsEntity & { isShowEdit: boolean })[]
export interface ApiItem {
	level: number
	MeetingMoid: number
	smsUserVO: MtEntity
}
function Translator(mt: MtEntity): SmsTerModel {
	return {
		mcc: new Mt(mt),
		status: {
			checked: false,
		},
		mtKey: mt.mt_key,
		number: '',
	}
}
export const ListTranslator = (list: MtEntity[]): SmsListModel => list.map((mt) => Translator(mt))
//校验添加的消息
export const checkMsg = (smsMsg: string) => {
	if (smsMsg.trim() == '') {
		Alert(t('短消息不能为空'))
		return false
	}
	if (smsMsg.trim().match(/\<|\>|"|'|\//gi)) {
		Alert(t('短消息不能包含敏感字符') + ' >\'<"\\/')
		return false
	}
	if (smsMsg.trim().length > 500) {
		Alert(t('短消息字符长度不能超过500'))
		return false
	}
	return true
}
//转换发送消息参数
export const getSmsParam = (
	msg: string,
	msgtype: number,
	rollTime: number | '',
	rollrate: number,
	checkLimit: boolean,
	mts?: string[]
) => {
	var param: SendSms = {
		msg: msg,
		msg_type: msgtype,
		roll_times: rollTime || 0,
		roll_rate: rollrate,
	}
	if (mts && mts.length > 0) {
		param.mts = mts
	}
	if (checkLimit) {
		param.roll_times = 255
	}
	if (param.msg_type === 3) {
		param.msg_type = 0
		param.roll_times = 0
		param.roll_rate = 0
	}
	return param
}
//校验数据
export const checkData = (smsMsg: string, roolTimesLimitl: boolean, rollTime: number | '', msgtype: number) => {
	if (smsMsg.trim() === '') {
		Alert(t('短消息不能为空'))
		return false
	}
	if (smsMsg.trim().match(/\<|\>|"|'|\//gi)) {
		Alert(t('短消息不能包含敏感字符') + ' >\'<"\\/')
		return false
	}
	if (smsMsg.length > 500) {
		Alert(t('短消息字符长度不能超过500'))
		return false
	}
	if (!roolTimesLimitl && msgtype !== 3) {
		var reg = /^\d+$/
		if (rollTime === 0) {
			Alert(t('请设置滚动次数'))
			return false
		}
		if (!reg.test(`${rollTime}`) || Number(rollTime) > 254 || Number(rollTime) < 1) {
			Alert(t('滚动次数范围为1~254次'))
			return false
		}
	}
	return true
}
//页面参数
export interface SmsPageEntity {
	list: SmsListModel
	receiVTerminal: number
	model: number
	speed: number
	showSpeedAndroll: boolean
	rollTime: number | ''
	rollTimeChecked: boolean
	smsList: SmsEntityList
}
//获取mtid数组
export const getMtIdList = (list: SmsListModel) => {
	return list.map((item) => item.mtKey)
}
//获取mtid对象
export const getMtId = (list: SmsListModel) => {
	return list.map((item) => ({ mtid: item.mtKey }))
}
//短消息输入框
export interface SmsTextArea {
	smsTextArea: string
	smsTextAreaModal: string
	smsTextAreaModalEdit: string
}
